package com.st.shopadmin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.st.shopadmin.common.ServerResponse;
import com.st.shopadmin.dao.TaskMapper;
import com.st.shopadmin.dao.TaskTaobaoKeywordsMapper;
import com.st.shopadmin.pojo.BuyerUserInfo;
import com.st.shopadmin.pojo.Shop;
import com.st.shopadmin.pojo.Task;
import com.st.shopadmin.pojo.TaskInfo;
import com.st.shopadmin.pojo.TaskTaobaoKeywords;
import com.st.shopadmin.pojo.TaskWithBLOBs;
import com.st.shopadmin.service.TaskService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TaskServiceImpl implements TaskService{

    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private TaskTaobaoKeywordsMapper taskTaobaoKeywordsMapper;


    @Override
    public ServerResponse<List> getTaskLists(int pageNum, int pageSize) {
        List<TaskInfo> buyerLists = taskMapper.getTaskLists();
        return  ServerResponse.createBySuccess(buyerLists);
    }


    @Override
    public ServerResponse<PageInfo> taskPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TaskInfo> taskLists = taskMapper.getTaskLists();
        PageInfo pageResult = new PageInfo(taskLists);
        return  ServerResponse.createBySuccess(pageResult);
    }

    @Override
    public ServerResponse<TaskInfo> taskDetail(Long taskId) {
        if(taskId == null){
            return  ServerResponse.createByErrorMessage("参数错误");
        }
        TaskInfo taskInfo = taskMapper.taskDetail(taskId);
        if(taskInfo == null){
            return  ServerResponse.createByErrorMessage("不存在");
        }
        return  ServerResponse.createBySuccess(taskInfo);
    }

    @Override
    public ServerResponse<List> taskKeywords(Long taskId) {
        if(null == taskId){
            return  ServerResponse.createByErrorMessage("参数错误");
        }
        List<TaskTaobaoKeywords> list = taskTaobaoKeywordsMapper.taskKeywords(taskId);
        if(list.size()<=0){
            return  ServerResponse.createByErrorMessage("不存在关键字");
        }
        return ServerResponse.createBySuccess(list);
    }

    @Override
    public ServerResponse checkTask(Long taskId, Integer status, String noPassReason) {
        if(status == null || taskId == null){
            return   ServerResponse.createByErrorMessage("参数错误");
        }
        TaskWithBLOBs task = new TaskWithBLOBs();
        task.setId(taskId);
        task.setStatus(status);
        if(null !=noPassReason && ""!=noPassReason){
            task.setNoPassReason(noPassReason);
        }else{
            task.setNoPassReason(null);
        }
        int rowCount  = taskMapper.updateByPrimaryKeySelective(task);
        if(rowCount > 0){
            return  ServerResponse.createBySuccess("更新成功");
        }
        return ServerResponse.createByErrorMessage("更新失败");
    }

    @Override
    public ServerResponse<List> searchLists(TaskInfo taskInfo,int pageNum,int pageSize){
        List<TaskInfo> productList = taskMapper.searchLists(taskInfo);
        return  ServerResponse.createBySuccess(productList);
    }


}
